package com.swm.rest;
/*
 * $Header: /home/cvspublic/jakarta-commons/httpclient/src/examples/PostXML.java,v 1.13 2004/05/12 20:43:53 olegk Exp $
 * $Revision: 1.13 $
 * $Date: 2004/05/12 20:43:53 $
 * ====================================================================
 *
 *  Copyright 2002-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePartSource;

/**
 *
 * This is a sample application that demonstrates
 * how to use the Jakarta HttpClient API.
 *
 * This application sends an XML document
 * to a remote web server using HTTP POST
 *
 * @author Sean C. Sullivan
 * @author Ortwin Gl?ck
 * @author Oleg Kalnichevski
 */
public class HttpPostMethod {

	private static HttpPostMethod postMethod = null;
	public static HttpPostMethod getInstance(){
		if ( postMethod == null ){
			postMethod = new HttpPostMethod();
		}
		return postMethod;
	}
	
	public ResultService request2( String url, String contents ) throws IOException{
        
        // Prepare HTTP post
        PostMethod method = new PostMethod( url );        
        
        InputStream is = new ByteArrayInputStream( contents.getBytes() );
        method.setRequestBody( is );        
        
        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        method.setRequestHeader("Content-type", "application/xml; charset=utf-8");

        // Get HTTP client
        HttpClient httpclient = new HttpClient();
        int statusCode = -1;
        // Execute request
        try {
        	statusCode = httpclient.executeMethod(method);
            System.out.println("Response status code: " + statusCode);
            System.out.println("Response body: ");
            System.out.println( method.getResponseBodyAsString() );
        } finally {
            // Release current connection to the connection pool once you are done
        	method.releaseConnection();
        }
        
        return new ResultService( statusCode, method.getResponseBodyAsString() );
    }
	
	public ResultService request( String url, String fileName ) throws IOException{

        // Get target URL
        // Get file to be posted                
        
        File input = new File( fileName );
        if ( !input.exists() )
        	return null;
        
        // Prepare HTTP post
        PostMethod method = new PostMethod( url );
        
        // Request content will be retrieved directly from the input stream
        // Per default, the request content needs to be buffered
        // in order to determine its length.
        // Request body buffering can be avoided when
        // content length is explicitly specified
        FilePartSource fps = new FilePartSource( input);

        //post.setRequestBody( fps.createInputStream());
        InputStream is = new ByteArrayInputStream( "A String....".getBytes() );


        
        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        method.setRequestHeader("Content-type", "application/xml; charset=utf-8");

        // Get HTTP client
        HttpClient httpclient = new HttpClient();
        int statusCode = -1;
        // Execute request
        try {
        	statusCode = httpclient.executeMethod(method);
            System.out.println("Response status code: " + statusCode);
            System.out.println("Response body: ");
            System.out.println(method.getResponseBodyAsString());
        } finally {
            // Release current connection to the connection pool once you are done
        	method.releaseConnection();
        }
        
        return new ResultService( statusCode, method.getResponseBodyAsString() );
    }
}
